package org.luxor.common.netty.protocol;

/**
 * Nett Message Types.
 *
 * @author Mr.yan @date 2024/1/31
 */
public enum NettyMessageType {
    CONNECT(1),
    CONN_ACK(2),
    PUBLISH(3),
    PUB_ACK(4),
    PUB_REC(5),
    PUB_REL(6),
    PUB_COMP(7),
    PING(8),
    PONG(9),
    DISCONNECT(10);

    private static final NettyMessageType[] VALUES;

    static {
        // this prevent values to be assigned with the wrong order
        // and ensure valueOf to work fine
        final NettyMessageType[] values = values();
        VALUES = new NettyMessageType[values.length + 1];
        for (NettyMessageType nettyMessageType : values) {
            final int value = nettyMessageType.value;
            if (VALUES[value] != null) {
                throw new AssertionError("value already in use: " + value);
            }
            VALUES[value] = nettyMessageType;
        }
    }

    private final int value;

    NettyMessageType(int value) {
        this.value = value;
    }

    public int value() {
        return value;
    }

    public static NettyMessageType valueOf(int type) {
        if (type <= 0 || type >= VALUES.length) {
            throw new IllegalArgumentException("unknown message type: " + type);
        }
        return VALUES[type];
    }
}
